package com.obtk.property.controller;


import com.obtk.property.dao.OwnerMapper;
import com.obtk.property.pojo.*;
import com.obtk.property.service.*;
import com.obtk.property.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户登录\获取菜单权限
 *
 * @author fly
 * @date 2021-12-08 20:54:09
 */
@Controller
public class WelcomeController {
    @Autowired
    private ManagerService managerService;
    @Autowired
    private CommunityService communityService;
    @Autowired
    private ComplaintService complaintService;
    @Autowired
    private ParkingService parkingService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private RepairService repairService;
    @Autowired
    private OwnerService ownerService;

    /**
     * 用户登录
     *
     * @param loginUser
     * @param session
     * @return
     */
    @RequestMapping("/loginUser")
    @ResponseBody
    public JsonResult login(Manager loginUser, HttpSession session) {
        //将接收到的密码 MD5加密  与数据库校对
        loginUser.setPassword(Md5Utils.MD5Encode(loginUser.getPassword(), "utf-8", false));
        Manager manager = managerService.login(loginUser.getLoginName(), loginUser.getPassword());
        //点击网址首页或logo跳转到首页
        System.out.println(manager);

        if (manager != null) {
            session.setAttribute("sysUser", manager);
            return new JsonResult(0, "登录成功");
        }
        return new JsonResult(1, "登录失败");

    }


    /**
     * 用户退出
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/login";
    }

    @RequestMapping("/welcome")
    public String test(HttpSession session) {
        int sum = 0;
        int sum1 = 0;
        List<Community> all = communityService.findAll();
        for (int a = 0; a < all.size(); a++) {
            sum = all.get(a).getTotalBuildings() + sum;
            sum1 = all.get(a).getTotalHouseholds() + sum1;
            session.setAttribute("name" + a, all.get(a).getCommunityName());//每个小区的名称
            session.setAttribute("building" + a, all.get(a).getTotalBuildings());//每个小区的栋数
            session.setAttribute("person" + a, all.get(a).getTotalHouseholds() * 3);//每个小区的人数=户*3
        }
        session.setAttribute("sum", sum);//总栋数
        session.setAttribute("sum1", sum1);//总户数
        //只显示十天最新数据

        for (int i = 0; i < parkingService.selAll().size(); i++) {
            session.setAttribute("parking" + i, parkingService.selAll().get(i).getCommunityName());
            session.setAttribute("parkingCommunity" + i, parkingService.sel(i + 1).size());//每个小区的车数量
        }
        List<String> stringList=new ArrayList<>();
        List<Integer> arrList=new ArrayList<>();
        for (int i = 0; i <complaintService.selB().size() ; i++) {
            stringList.add(complaintService.selB().get(i).getCommunityName());
            arrList.add(complaintService.selC(complaintService.selB().get(i).getCommunityName()).size());
        }
        session.setAttribute("arrList",arrList);
        session.setAttribute("stringList",stringList);
        List<String> list4=new ArrayList<>();
        List<String> list5=new ArrayList<>();//createTime
        for (int i = 0; i <10 ; i++) {
            list4.add(activityService.selA().get(i).getTitle());
            list5.add(activityService.selA().get(i).getCreateTime());
        }
//            session.setAttribute("",);
        session.setAttribute("list4",list4);//活动名称
        session.setAttribute("list5",list5);//活动名称
        session.setAttribute("complaint", complaintService.findAll().size());
        repairService.findAll();
        List<String> list2=new ArrayList<>();
        List<String> list3=new ArrayList<>();
        //只显示十天最新数据
        for (int i = 0; i < 10; i++) {
            list2.add(repairService.list().get(i).getDeviceName());
            list3.add(repairService.list().get(i).getCreateTime());
        }
        session.setAttribute("list2",list2);
        session.setAttribute("list3",list3);

       session.setAttribute("sum3",ownerService.selCount(new Owner()));
        List<String> sta=new ArrayList<>();
        List<String> sta1=new ArrayList<>();
        for (int i = 0; i <repairService.findAll().size() ; i++) {
            if (repairService.findAll().get(i).getStatus().equals("1")){
                if (sta.size()<=9){
                sta.add(repairService.findAll().get(i).getDeviceName());
                sta1.add(repairService.findAll().get(i).getCreateTime());
                }
            }
        }
        session.setAttribute("sta",sta);
        session.setAttribute("sta1",sta1);
        int typeNum=0;
        for (int i = 0; i <ownerService.findAll().size() ; i++) {
            if (ownerService.findAll().get(i).getType().equals("1")){
                typeNum++;
            }
        }
        session.setAttribute("typeMum",typeNum);

        return "/welcome";


    }
}
